package com.example.love_margin.Controller;


import com.example.love_margin.Entity.Pet;
import com.example.love_margin.Entity.Until.PageInfo;
import com.example.love_margin.Service.PetService;
import com.example.love_margin.Untils.ResultCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;

@Api(value = "宠物信息管理",tags = "管理宠物")
@RestController
@RequestMapping(value = "/Pet" ,name = "宠物信息")
@CrossOrigin
public class PetController {

    @Autowired
    PetService petService;

    //前台功能
    //查询单个宠物信息
    @ApiOperation(value = "查询单个宠物信息",notes = "查询单个宠物信息")
    @GetMapping(value = "/selectOnePet")
    public Object selectOnePet(@RequestParam int pet_id) {
        return petService.selectOnePet(pet_id);
    }

    //后台功能，上面查看单个宠物的也是
    //增加宠物
    @ApiOperation(value = "增加宠物",notes = "增加宠物")
    @PostMapping(value = "/addPet")
    public Object addPet(@RequestBody Pet pet,@RequestParam(required = false) String x){
        return petService.addPet(pet);
    }


    //删除单个宠物
    @ApiOperation(value = "删除单个宠物",notes = "删除单个宠物")
    @PostMapping(value = "/deletPet")
    public Object deletePet(@RequestParam int pet_id){
        return petService.deletePet(pet_id);
    }

    /*
批量删除
*/
    @ApiOperation(value = "删除多个宠物", notes = "删除多个宠物")
    @PostMapping(value = "/deletPets")
    public Object deletePets(@RequestBody Integer[] pet_ids) {
        System.out.println(Arrays.toString(pet_ids));
        return petService.delPets(pet_ids);
    }


    //修改单个宠物信息
    @ApiOperation(value = "修改单个宠物信息",notes = "修改单个宠物信息")
    @PostMapping(value = "/updatePet")
    public Object updatePet(@RequestBody Pet pet) {

        return petService.updateOnePet(pet);
    }


    //多条件查询，包括关键字    //分页查询，默认查询全部宠物（为领养和已领养），需要加地区的条件
    @ApiOperation(value = "多条件查询",notes = "类别，品种，性别，关键词，页码，每页条数,无参数时只返回10条数据")
    @GetMapping(value = "/selectManyPage")
    public Object selectManyPage(@RequestParam(required = false) String pet_category,
                                 @RequestParam(required = false) String pet_varieties,
                                 @RequestParam(required = false) String pet_sex,
                                 @RequestParam(required = false) String key,
                                 @RequestParam(required = false) Integer pageNum,
                                 @RequestParam(required = false) Integer pageSize) {

            List<Pet> pet = petService.selectManyPage(pet_category, pet_varieties, pet_sex, key,pageNum,pageSize);
            // 需要把Page包装成PageInfo对象才能序列化。该插件也默认实现了一个PageInfo
            PageInfo<Pet> pageInfo = new PageInfo<>(pet);
            return ResultCode.SUCCESS.result(pageInfo);
    }











}
